package com.sport.facilities.booking.system.htmlchecker;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import android.util.Log;

import com.sport.facilities.booking.system.core.HtmlHelper;
import com.sport.facilities.booking.system.model.Booking;
import com.sport.facilities.booking.system.model.MakeBookFormModel;

public class MakeBookingCheckChecker extends BasicChecker {
	private final String TAG = "MakeBookingCheckChecker";
	
	public ArrayList<Booking> fnGetBookingListFromHtml(String html, MakeBookFormModel bm)
			throws ParserException, Exception {	
		
		fnFindHiddenFormValue(html,bm);
		
		ArrayList<Booking> bookingList = new ArrayList<Booking>();
		NodeList tableNodeList = HtmlHelper.getNodeListByTag(html,
		"table");
		TagNameFilter fontFilter = new TagNameFilter("FONT");
		HasAttributeFilter hasSizeAttribute = new HasAttributeFilter("size");
		AndFilter fontHasSizeFilter = new AndFilter(fontFilter, hasSizeAttribute);
		NodeList foundFontHasSizeNodeList = tableNodeList.extractAllNodesThatMatch(
				fontHasSizeFilter, true);

		NodeList inputnodelist = HtmlHelper.getNodeListByTag(
				html, "div");
		TagNameFilter inputFilter = new TagNameFilter("INPUT");
		HasAttributeFilter hasValueAttribute = new HasAttributeFilter("value");
		AndFilter hasValInpFilter = new AndFilter(inputFilter, hasValueAttribute);
		NodeList inputfoundNodeList = inputnodelist.extractAllNodesThatMatch(
				hasValInpFilter, true);
		/*
		 * Total node number should be :
		 * total node = 5{TD} * ((row of record) + (header row)) + 3( Header Text)
		 * */
		if (foundFontHasSizeNodeList.size() == 0 || (!((foundFontHasSizeNodeList.size()-3)%5 == 0))) {
			throw new Exception("The session is NOT available.\n");
		}
		
		int j = 0;
		for (int i = 0; i < foundFontHasSizeNodeList.size(); i++) {
			if (i <= 6 || i % 5 != 2 || i + 1 == foundFontHasSizeNodeList.size()) {
				continue;
			}

			Booking booking = new Booking();

			String radioValue = inputfoundNodeList.elementAt(j).toHtml();
			radioValue = radioValue.replaceAll("\\D+", "");
			Node dateNode = foundFontHasSizeNodeList.elementAt(i).getNextSibling();
			Node timeNode = foundFontHasSizeNodeList.elementAt(i + 1).getNextSibling();
			Node centreNode = foundFontHasSizeNodeList.elementAt(i + 2)
					.getNextSibling();
			Node courtNode = foundFontHasSizeNodeList.elementAt(i + 3)
					.getNextSibling();
			Node activityNode = foundFontHasSizeNodeList.elementAt(i + 4)
					.getNextSibling();

			if (i % 5 == 2) {
				j++;
			}
			booking.setId(radioValue);
			booking.setDate(dateNode.getText());
			booking.setTime(timeNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setCentre(centreNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setPrice(activityNode.getText());
			booking.setActivity(bm.getActivity());
			bookingList.add(booking);
		}
		Log.d(TAG, "Booking record Found : "+bookingList.size());
		return bookingList;
	}
	
	private void fnFindHiddenFormValue(String html, MakeBookFormModel bm) throws ParserException {
		NodeList inputnodelist = HtmlHelper.getNodeListByTag(html, "INPUT");
		HasAttributeFilter hasValueAttribute = new HasAttributeFilter("type");
		NodeList inputTypeNodeList = inputnodelist.extractAllNodesThatMatch(
				hasValueAttribute, true);
		for (int i = 0; i < inputTypeNodeList.size(); i++) {
			Node n = inputTypeNodeList.elementAt(i);
			if(n.getText().contains("hidden")){
				Tag tag = (Tag) n;
				String name = tag.getAttribute("name");
				String val = tag.getAttribute("value");
				bm.setActivityCodeName(name);
				bm.setActivityCodeValue(val);
			}
		}
	}
}